{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "######################## str方法系列 ##################################\n",
    "test_str=\"ABCDabcdefacddbdf\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6\n",
      "11\n",
      "-1\n"
     ]
    }
   ],
   "source": [
    "# 查找:find,rfind,index,rindex\n",
    "# Python查找推荐你用find和rfind【xxx.find(str, start, end)】\n",
    "\n",
    "print(test_str.find(\"cd\"))#从左往右\n",
    "print(test_str.rfind(\"cd\"))#从右往左\n",
    "print(test_str.find(\"dnt\"))#find和rfind找不到就返回-1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "substring not found",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-3-cc6e22ad9acc>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[0;31m# index和rindex用法和find一样，只是找不到会报错（以后用find系即可）\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtest_str\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"dnt\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mValueError\u001b[0m: substring not found"
     ]
    }
   ],
   "source": [
    "# index和rindex用法和find一样，只是找不到会报错（以后用find系即可）\n",
    "print(test_str.index(\"dnt\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4\n",
      "2\n"
     ]
    }
   ],
   "source": [
    "# 计数:count\n",
    "# xxx.count(str, start, end)\n",
    "\n",
    "print(test_str.count(\"d\"))#4\n",
    "print(test_str.count(\"cd\"))#2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ABCDabcdefacddbdf\n",
      "ABCDaBcdefacddBdf\n",
      "ABCDabcdefacddbdf\n"
     ]
    }
   ],
   "source": [
    "# 替换:replace\n",
    "# xxx.replace(str1, str2, 替换次数)\n",
    "\n",
    "print(test_str)\n",
    "print(test_str.replace(\"b\",\"B\"))#并没有改变原字符串，只是生成了一个新的字符串\n",
    "print(test_str)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ABCDaBcdefacddbdf\n"
     ]
    }
   ],
   "source": [
    "# replace可以指定替换几次\n",
    "print(test_str.replace(\"b\",\"B\",1))#ABCDaBcdefacddbdf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "######################## str分割系列 ##################################"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['ABCD', 'bcdef', 'cddbdf']\n"
     ]
    }
   ],
   "source": [
    "# 分割:split（按指定字符分割）,splitlines(按行分割),partition(以str分割成三部分,str前，str和str后),rpartition\n",
    "test_list=test_str.split(\"a\")#a有两个，按照a分割，那么会分成三段，返回类型是列表（List），并且返回结果中没有a\n",
    "print(test_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['hi', 'my', 'name', 'is', 'dnt']\n",
      "['hi', 'my', 'name', 'is dnt']\n"
     ]
    }
   ],
   "source": [
    "test_input=\"hi my name is dnt\"\n",
    "print(test_input.split(\" \")) #返回列表格式（后面会说）['hi', 'my', 'name', 'is', 'dnt']\n",
    "print(test_input.split(\" \",3))#在第三个空格处切片，后面的不管了"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['abc', 'bca', 'cab']\n",
      "['abc', 'bca', 'cab', '']\n"
     ]
    }
   ],
   "source": [
    "# splitlines()按行分割，返回类型为List\n",
    "test_line_str=\"abc\\nbca\\ncab\\n\"\n",
    "print(test_line_str.splitlines())#['abc', 'bca', 'cab']\n",
    "print(test_line_str.split(\"\\n\"))#看出区别了吧：['abc', 'bca', 'cab', '']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['abc', 'bca', 'cab', 'LLL']\n",
      "['abc', 'bca', 'cab', 'LLL']\n"
     ]
    }
   ],
   "source": [
    "# # splitlines(按行分割)，和split(\"\\n\")的区别没看出来就再来个案例\n",
    "test_line_str2=\"abc\\nbca\\ncab\\nLLL\"\n",
    "print(test_line_str2.splitlines())#['abc', 'bca', 'cab', 'LLL']\n",
    "print(test_line_str2.split(\"\\n\"))#再提示一下，最后不是\\n就和上面一样效果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['hi', 'my', 'name', 'is', 'dnt', 'm']\n"
     ]
    }
   ],
   "source": [
    "# 扩展：split()，默认按空字符切割(空格、\\t、\\n等等，不用担心返回'')\n",
    "print(\"hi my name is dnt\\t\\n  m\\n\\t\\n\".split())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('ABCDab', 'cd', 'efacddbdf')\n",
      "('ABCDabcdefa', 'cd', 'dbdf')\n",
      "('ABCDabcdefacddbdf', '', '')\n"
     ]
    }
   ],
   "source": [
    "# partition(以str分割成三部分,str前，str和str后)\n",
    "# 返回是元祖类型（后面会说的）,方式和find一样，找到第一个匹配的就罢工了【注意一下没找到的情况】\n",
    "\n",
    "print(test_str.partition(\"cd\"))#('ABCDab', 'cd', 'efacddbdf')\n",
    "print(test_str.rpartition(\"cd\"))#('ABCDabcdefa', 'cd', 'dbdf')\n",
    "print(test_str.partition(\"感觉自己萌萌哒\"))#没找到：('ABCDabcdefacddbdf', '', '')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "######################## str方法系列 ##################################"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ABCD-bcdef-cddbdf\n"
     ]
    }
   ],
   "source": [
    "# 连接：join\n",
    "# separat.join(xxx)\n",
    "# 错误用法：xxx.join(\"-\")\n",
    "print(\"-\".join(test_list))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "# 头尾判断:startswith（以。。。开头）,endswith（以。。。结尾）\n",
    "# test_str.startswith（以。。。开头）\n",
    "start_end_str=\"http://www.baidu.net\"\n",
    "print(start_end_str.startswith(\"https://\") or start_end_str.startswith(\"http://\"))\n",
    "print(start_end_str.endswith(\".com\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ABCDabcdefacddbdf\n",
      "ABCDABCDEFACDDBDF\n",
      "abcdabcdefacddbdf\n",
      "Abcdabcdefacddbdf\n"
     ]
    }
   ],
   "source": [
    "# 大小写系:lower(字符串转换为小写),upper(字符串转换为大写)\n",
    "# title(单词首字母大写),capitalize(第一个字符大写，其他变小写)\n",
    "\n",
    "print(test_str)\n",
    "print(test_str.upper())#ABCDABCDEFACDDBDF\n",
    "print(test_str.lower())#abcdabcdefacddbdf\n",
    "print(test_str.capitalize())#第一个字符大写，其他变小写"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I Have a Dream|\n",
      "I Have a Dream |\n",
      " I Have a Dream|\n",
      "ABCDabcdefacddbdf                                 \n",
      "                                 ABCDabcdefacddbdf\n",
      "                ABCDabcdefacddbdf                 \n"
     ]
    }
   ],
   "source": [
    "# 格式系列：lstrip（去除左边空格）,rstrip（去除右边空格）,strip（去除两边空格）美化输出系列：ljust,rjust,center\n",
    "strip_str=\" I Have a Dream \"\n",
    "print(strip_str.strip()+\"|\")#我加 | 是为了看清后面空格，没有别的用处\n",
    "print(strip_str.lstrip()+\"|\")\n",
    "print(strip_str.rstrip()+\"|\")\n",
    "\n",
    "#这个就是格式化输出，就不讲了\n",
    "print(test_str.ljust(50))\n",
    "print(test_str.rjust(50))\n",
    "print(test_str.center(50))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "######################## str验证系列 ##################################"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 验证系列:isalpha（是否是纯字母）,isalnum（是否是数字|字母）,isdigit（是否是纯数字）,isspace（是否是纯空格）\n",
    "# 注意哦~ test_str5=\" \\t \\n \" #isspace() ==>true\n",
    "\n",
    "test_str2=\"Abcd123\"\n",
    "test_str3=\"123456\"\n",
    "test_str4=\"  \\t\"     #isspace() ==>true\n",
    "test_str5=\"  \\t \\n \" #isspace() ==>true"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test_str.isalpha() #是否是纯字母"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test_str.isalnum() #是否是数字|字母"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test_str.isdigit() #是否是纯数字"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test_str.isspace() #是否是纯空格"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test_str2.isalnum() #是否是数字和字母组成"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test_str2.isdigit() #是否是纯数字"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test_str3.isdigit() #是否是纯数字"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test_str5.isspace() #是否是纯空格"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test_str4.isspace() #是否是纯空格"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
